package com.kds.changzhou.dao;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * spring redis 工具类
 *
 * @author ruoyi
 **/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Slf4j
public class RedisCache {
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 获取所有数据
     * @return Map<fid, value>
     */
    public Map<String, Double> getAllData() {
        Map<String, Double> result = new HashMap<>();
        try {
            Set<String> keys = redisTemplate.keys("*");  // 获取所有键
            if (keys != null) {
                for (String key : keys) {
                    Object value = redisTemplate.opsForValue().get(key);
                    if (value != null && value instanceof Number) {
                        result.put(key, ((Number) value).doubleValue());
                    }
                }
            }
            log.info("成功获取 {} 条Redis数据", result.size());
        } catch (Exception e) {
            log.error("获取Redis数据失败", e);
        }
        return result;
    }

    /**
     * 获取指定建筑物的所有数据
     * @param buildingId 建筑ID
     * @return Map<fid, value>
     */
    public Map<String, Double> getBuildingData(String buildingId) {
        Map<String, Double> result = new HashMap<>();
        try {
            Set<String> keys = redisTemplate.keys(buildingId + "_*");
            if (keys != null) {
                for (String key : keys) {
                    Object value = redisTemplate.opsForValue().get(key);
                    if (value != null && value instanceof Number) {
                        result.put(key, ((Number) value).doubleValue());
                    }
                }
            }
            log.info("成功获取建筑 {} 的 {} 条Redis数据", buildingId, result.size());
        } catch (Exception e) {
            log.error("获取建筑 {} 的Redis数据失败", buildingId, e);
        }
        return result;
    }

    /**
     * 获取指定网关的所有数据
     * @param buildingId 建筑ID
     * @param gatewayId 网关ID
     * @return Map<fid, value>
     */
    public Map<String, Double> getGatewayData(String buildingId, String gatewayId) {
        Map<String, Double> result = new HashMap<>();
        try {
            Set<String> keys = redisTemplate.keys(buildingId + "_" + gatewayId + "_*");
            if (keys != null) {
                for (String key : keys) {
                    Object value = redisTemplate.opsForValue().get(key);
                    if (value != null && value instanceof Number) {
                        result.put(key, ((Number) value).doubleValue());
                    }
                }
            }
            log.info("成功获取网关 {}/{} 的 {} 条Redis数据", buildingId, gatewayId, result.size());
        } catch (Exception e) {
            log.error("获取网关 {}/{} 的Redis数据失败", buildingId, gatewayId, e);
        }
        return result;
    }
}
